package com.aoip.telemetry.telemetry.quartz.task;


import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.entity.common.OriginalMessageEntity;
import com.aoip.telemetry.telemetry.entity.constant.TelemetryConstants;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.aoip.telemetry.telemetry.entity.equipment.StInfoConfigEntity;
import com.aoip.telemetry.telemetry.entity.equipment.StOnlineStatusEntity;
import com.aoip.telemetry.telemetry.quartz.mapper.OriginalMessageTaskMapper;
import com.aoip.telemetry.telemetry.quartz.mapper.StOnlineStatusTaskMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 测站在线状态
 * 最近一小时有数据上报，即为在线，否则非在线
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/3/19 20:31
 */
@Component
@Slf4j
public class StOnlineStatusTask {

    @Resource
    private RedisCache redisCache;
    @Resource
    private StOnlineStatusTaskMapper mapper;
    @Resource
    private OriginalMessageTaskMapper originalMapper;

    private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public void task() {
        try {
            LocalDateTime now = LocalDateTime.now();
            Collection<String> keys = redisCache.keys(CacheKeyEnum.STATION + ":*");
            List<StInfoConfigEntity> stList = new ArrayList<>();
            keys.forEach(item -> {
                StInfoConfigEntity stInfo = redisCache.getCacheObject(item);
                stList.add(stInfo);
            });
            String key = "st_online_last_time";
            LocalDateTime last = now.minusHours(1);
            if (redisCache.hasKey(key)) {
                String lastTimeStr = redisCache.getCacheObject(key);
                last = LocalDateTime.parse(lastTimeStr, dtf);
            }
            log.error("last11={}", last);
            QueryWrapper<OriginalMessageEntity> originalEntityQueryWrapper = new QueryWrapper<>();
            originalEntityQueryWrapper.ge("create_time", last);
            originalEntityQueryWrapper.lt("create_time", now);
            List<OriginalMessageEntity> originalEntityList = originalMapper.selectList(originalEntityQueryWrapper);
            List<StOnlineStatusEntity> lineList = new ArrayList<>();
            Set<String> offlineSet = new HashSet<>();
            log.error("originalEntityList=={}", originalEntityList.size());
            stList.forEach(item -> {
                String stCd = item.getStCd();
                offlineSet.add(stCd);
                for (OriginalMessageEntity entity : originalEntityList) {
                    if (entity == null || !StringUtils.hasText(entity.getStCode())) {
                        continue;
                    }
                    if (stCd.contains(entity.getStCode())) {
                        StOnlineStatusEntity online = new StOnlineStatusEntity();
                        online.setId(IdWorker.getIdStr(online));
                        online.setStCode(stCd);
                        online.setOnlineStatus(TelemetryConstants.ONLINE);
                        online.setOnlineTime(now);
                        lineList.add(online);
                        offlineSet.remove(stCd);
                    }
                }
            });
            offlineSet.forEach(item -> {
                StOnlineStatusEntity online = new StOnlineStatusEntity();
                online.setId(IdWorker.getIdStr(online));
                online.setStCode(item);
                online.setOnlineStatus(TelemetryConstants.OFFLINE);
                online.setOnlineTime(now);
                lineList.add(online);
            });
            log.error("stList=={};lineList=={}", stList.size(), lineList.size());
            mapper.save(lineList);
            redisCache.setCacheObject(key, now.format(dtf));
            log.error("last11={}", now.format(dtf));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        System.out.println(encoder.encode("123456"));
    }
}
